package com.dream.plugin.simpledao.util;

import com.dream.plugin.simpledao.exception.NotAnnotationWithSTableException;
import com.dream.plugin.simpledao.tag.SColumn;
import com.dream.plugin.simpledao.tag.SId;
import com.dream.plugin.simpledao.tag.STable;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

public class SStringUtil {

    public static Boolean isEmpty(String string){
        return string == null || "".equals(string);
    }

    // 将大写字母变为小写，并在第一字母以后的大写字母前加 '_'
    public static String formatNameInDB(String string){
        char first = string.charAt(0);
        StringBuilder sbTbName = new StringBuilder( first >= 'A' && first <= 'Z' ? first + 32 : first );
        for (int i = 1; i < string.length() ; i ++){
            char c = string.charAt(i);
            if(c >= 'A' && c <= 'Z'){
                sbTbName.append("_");
                c += 32;
            }
            sbTbName.append(c);
        }
        return sbTbName.toString();
    }

    /**
     * 获取被@STable注解类的对应的数据库表名
     * @param table
     * @return
     */
    public static String getNameOfTable(Class table){
        STable sTable = (STable) table.getAnnotation(STable.class);
        String annotationVal = sTable.value();
        return "".equals(annotationVal) ? formatNameInDB(table.getSimpleName()) : annotationVal;
    }

    /**
     * 获取被@SColumn注解的字段
     * @param field
     * @return
     */
    public static String getNameOfColumn(Field field){
        SColumn sColumn = (SColumn) field.getAnnotation(SColumn.class);
        String annotationVal = sColumn.value();
        return "".equals(annotationVal) ? formatNameInDB(field.getName()) : annotationVal;
    }

    /**
     * 获取被@Sid注解的值
     * @param field
     * @return
     */
    public static String getNameOfId(Field field){
        SId sId = field.getAnnotation(SId.class);
        String annotationVal = sId.value();
        return "".equals(annotationVal) ? formatNameInDB(field.getName()) : annotationVal;
    }

    /**
     * 获取实体类的数据
     * @param pojo
     * @return
     * @throws IllegalAccessException
     */
    public static Map<String,Object> getTableData(Object pojo, Class clazz) throws IllegalAccessException {
        Class table = clazz;
        if (! table.isAnnotationPresent(STable.class)){
            throw new NotAnnotationWithSTableException();
        }
        HashMap<String, Object> data = new HashMap<>();
        Field[] fields = table.getDeclaredFields();
        for (Field field : fields){
            String columnName;
            if (field.isAnnotationPresent(SId.class)){
                columnName = getNameOfId(field);
            }else if (field.isAnnotationPresent(SColumn.class)){
                columnName = getNameOfColumn(field);
            }else {
                continue;
            }
            field.setAccessible(true);
            Object value = field.get(pojo);
            data.put(columnName, value);
        }
        return data;
    }
}
